
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Atlas之Bundle加载过程 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    
    <link rel="prev" href="../atlas_start/atlas_start_2.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    原理解析
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../../principle-intro/Runtime_principle.html">
            
                <a href="../../principle-intro/Runtime_principle.html">
            
                    
                    框架原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../../principle-intro/Project_architectured.html">
            
                <a href="../../principle-intro/Project_architectured.html">
            
                    
                    名词解释
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../../principle-intro/Apk_architecture.html">
            
                <a href="../../principle-intro/Apk_architecture.html">
            
                    
                    APK结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../../principle-intro/File_architecture_runtime.html">
            
                <a href="../../principle-intro/File_architecture_runtime.html">
            
                    
                    运行期文件结构
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../../guide-for-use/">
            
                <a href="../../guide-for-use/">
            
                    
                    接入指引
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../../guide-for-use/guide_for_build.html">
            
                <a href="../../guide-for-use/guide_for_build.html">
            
                    
                    容器接入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../../guide-for-use/guide_for_bundle.html">
            
                <a href="../../guide-for-use/guide_for_bundle.html">
            
                    
                    bundle拆分|新建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../../guide-for-use/guide_for_compile.html">
            
                <a href="../../guide-for-use/guide_for_compile.html">
            
                    
                    awo编译
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../../guide-for-use/bundleCommunicate.html">
            
                <a href="../../guide-for-use/bundleCommunicate.html">
            
                    
                    跨bundle的代码重用和通信
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../../update/">
            
                <a href="../../update/">
            
                    
                    动态部署
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../../update/principle.html">
            
                <a href="../../update/principle.html">
            
                    
                    技术原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../../update/dexpatch.html">
            
                <a href="../../update/dexpatch.html">
            
                    
                    dexpatch
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../../update/dexpatch_use_guide.html">
            
                <a href="../../update/dexpatch_use_guide.html">
            
                    
                    dexpatch使用教程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../../update/guide.html">
            
                <a href="../../update/guide.html">
            
                    
                    一些限制
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    FAQ
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../../faq/question.html">
            
                <a href="../../faq/question.html">
            
                    
                    问答
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../../faq/help.html">
            
                <a href="../../faq/help.html">
            
                    
                    故障排查
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../../faq/variant.html">
            
                <a href="../../faq/variant.html">
            
                    
                    构建定制包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../../faq/dynamic_failed_help.html">
            
                <a href="../../faq/dynamic_failed_help.html">
            
                    
                    动态部署失败排查指南
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    源码分析
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../atlas_gradle_apk/atlas_atlas_gradle_apk.html">
            
                <a href="../atlas_gradle_apk/atlas_atlas_gradle_apk.html">
            
                    
                    Atlas之Gradle配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../atlas_start/atlas_start_1.html">
            
                <a href="../atlas_start/atlas_start_1.html">
            
                    
                    Atlas之启动过程(一)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../atlas_start/atlas_start_2.html">
            
                <a href="../atlas_start/atlas_start_2.html">
            
                    
                    Atlas之启动过程(二)
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.6.4" data-path="atlas_bundle_load.html">
            
                <a href="atlas_bundle_load.html">
            
                    
                    Atlas之Bundle加载过程
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >Atlas之Bundle加载过程</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x6982;&#x8FF0;">&#x6982;&#x8FF0;</h1>
<p>atals&#x4E2D;bundle&#x52A0;&#x8F7D;&#x8FC7;&#x7A0B;</p>
<ul>
<li>class</li>
<li>resource</li>
</ul>
<p><img src="bundle_load_img.svg" alt=""></p>
<p><strong>&#x6CE8;&#x610F;&#x56FE;&#x4E2D;&#x7684;&#x7BAD;&#x5934;</strong></p>
<ul>
<li>&#x5B9E;&#x7EBF;&#x7BAD;&#x5934;&#x8868;&#x793A;&#x8FD0;&#x884C;&#x5728;&#x4E3B;&#x7EBF;&#x7A0B;</li>
<li>&#x865A;&#x7EBF;&#x7BAD;&#x5934;&#x8868;&#x793A;&#x8FD0;&#x884C;&#x5728;HandlerThread&#x4E2D;</li>
</ul>
<p>&#x5C06;&#x6D41;&#x7A0B;&#x5206;&#x4E3A;&#x4E09;&#x5927;&#x90E8;&#x5206;</p>
<table>
<thead>
<tr>
<th>&#x90E8;&#x5206;</th>
<th>&#x8BF4;&#x660E;</th>
<th>&#x5BF9;&#x5E94;&#x6B65;&#x9AA4;</th>
</tr>
</thead>
<tbody>
<tr>
<td>&#x7B2C;&#x4E00;&#x90E8;&#x5206;</td>
<td>&#x52A0;&#x8F7D;&#x65F6;&#x673A;&#x7684;&#x89E6;&#x53D1;&#x903B;&#x8F91;</td>
<td>1-7</td>
</tr>
<tr>
<td>&#x7B2C;&#x4E8C;&#x90E8;&#x5206;</td>
<td>bundle&#x52A0;&#x8F7D;&#x8FC7;&#x7A0B;</td>
<td>8-15</td>
</tr>
<tr>
<td>&#x7B2C;&#x4E09;&#x90E8;&#x5206;</td>
<td>bundle&#x4EE3;&#x7801;&#x521D;&#x59CB;&#x5316;</td>
<td>16-19</td>
</tr>
</tbody>
</table>
<h1 id="&#x7B2C;&#x4E00;&#x90E8;&#x5206;-&#x89E6;&#x53D1;&#x65F6;&#x673A;">&#x7B2C;&#x4E00;&#x90E8;&#x5206; &#x89E6;&#x53D1;&#x65F6;&#x673A;</h1>
<h2 id="&#x5165;&#x53E3;">&#x5165;&#x53E3;</h2>
<p>&#x4EE5;&#x5728;MainActivity&#x4E2D;&#x70B9;&#x51FB;secondebundle&#x4E3A;&#x4F8B;</p>
<p>MainActivity.java</p>
<pre><code>case R.id.navigation_dashboard:
    switchToActivity(&quot;second&quot;,&quot;com.taobao.secondbundle.SecondBundleActivity&quot;)
</code></pre><p>switchActivity &#x8F97;&#x8C03;&#x7528;&#xFF0C;&#x4F1A;&#x6267;&#x884C;&#x5230;&#x7B2C;2&#x6B65;<code>execStartChildActivityInternal</code>&#x65B9;&#x6CD5;&#x4E2D;</p>
<h2 id="execstartchildactivityinternal">execStartChildActivityInternal</h2>
<p>ActivityGroupDelegate.java</p>
<pre><code>public void execStartChildActivityInternal(ViewGroup container,String key, Intent intent){
    String bundleName = AtlasBundleInfoManager.instance().getBundleForComponet(componentName);
    if(!TextUtils.isEmpty(bundleName)){
        BundleImpl impl = (BundleImpl) Atlas.getInstance().getBundle(bundleName);
        if(impl!=null&amp;&amp;impl.checkValidate()) {    }else {
        //...
        asyncStartActivity(container,key,bundleName,intent);
    }
}
</code></pre><ul>
<li>&#x9996;&#x5148;&#xFF0C;&#x6839;&#x636E;componentName&#x5373;<code>com.taobao.secondbundle.SecondBundleActivity</code>,&#x67E5;&#x8BE2;bundle&#x7684;&#x540D;&#x79F0;&#x3002;&#x5728;<a href="../atlas_start/atlas_start_2.html">atlas&#x542F;&#x52A8;&#x8FC7;&#x7A0B;(&#x4E0B;)</a>&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x77E5;&#x9053;AtlasBundleInfoManager&#x4E2D;&#x5B58;&#x50A8;&#x4E86;&#x51E0;&#x4E4E;&#x6240;&#x6709;bundle&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x6240;&#x4EE5;&#x8FD4;&#x56DE; bundleName &#x5C31;&#x662F; <code>com.taobao.secondbundle</code>&#x3002;</li>
<li>&#x7136;&#x540E;&#xFF0C;&#x6839;&#x636E;bundlename&#xFF0C;&#x53BB;&#x67E5;&#x8BE2;bundle&#x52A0;&#x8F7D;&#x540E;&#x7684;&#x7ED3;&#x6784;&#x4F53;- <code>BundleImpl</code>&#x3002;&#x7531;&#x4E8E;secondbundle&#x4E4B;&#x524D;&#x5E76;&#x6CA1;&#x6709;&#x52A0;&#x8F7D;&#x8FDB;&#x6765;&#xFF0C;&#x6240;&#x4EE5;&#x4F1A;&#x6267;&#x884C;&#x5230;&#x7B2C;3&#x6B65;<code>asyncStartActivity</code>&#x65B9;&#x6CD5;&#x3002;</li>
</ul>
<p>asyncStartActivity&#x6700;&#x7EC8;&#x8C03;&#x7528;&#x4E86;BundleUtil&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x6211;&#x4EEC;&#x76F4;&#x63A5;&#x770B;&#x7B2C;4&#x6B65; <code>checkBundleStateAsync</code></p>
<h2 id="checkbundlestateasync">checkBundleStateAsync</h2>
<p>BundleUtil.java</p>
<pre><code>public static boolean checkBundleArrayStateAsync(final String[] bundlesName, final Runnable bundleActivated, final Runnable bundleDisabled){
    BundleInstaller installer = BundleInstallerFetcher.obtainInstaller();
    installer.installTransitivelyAsync(bundlesName, new BundleInstaller.InstallListener() {
        @Override
        public void onFinished() {
            boolean success = true;
            BundleImpl tmp;
            for(String bundleName : bundlesName){
                if((tmp=((BundleImpl) Atlas.getInstance().getBundle(bundleName)))==null || !tmp.checkValidate()){
                    success = false;
                }else{
                    tmp.startBundle();
                }
          }
       });
    return true;
}
</code></pre><p>&#x5728;&#x8FD9;&#x4E00;&#x6B65;&#x4E2D;&#xFF0C;&#x5F00;&#x59CB;&#x5F02;&#x6B65;&#x52A0;&#x8F7D;bundle&#xFF0C;&#x6210;&#x529F;&#x540E;&#xFF0C;&#x5728;&#x4E3B;&#x7EBF;&#x7A0B;&#x4E2D;&#x56DE;&#x8C03;<code>onFinished</code>(&#x540E;&#x9762;&#x4F1A;&#x63D0;&#x53CA;),&#x8C03;&#x7528;BundleImpl&#x5BF9;&#x8C61;&#x7684;startBundl&#x65B9;&#x6CD5;&#xFF0C;&#x5F00;&#x59CB; <strong>&#x7B2C;&#x4E09;&#x90E8;&#x5206;</strong> &#x7684;&#x521D;&#x59CB;&#x5316;&#x8FC7;&#x7A0B;&#x3002; </p>
<h2 id="deliverytask">deliveryTask</h2>
<p>&#x7B2C;5&#x3001;6&#x6B65;&#xFF0C;&#x4E3B;&#x8981;&#x662F;&#x5404;&#x79CD;&#x903B;&#x8F91;&#x5224;&#x65AD;&#xFF0C;&#x4E4B;&#x540E;&#x8F97;&#x8F6C;&#x8C03;&#x7528;&#x5230;7&#x6B65;&#x3002;&#x76F4;&#x63A5;&#x770B;&#x7B2C;7&#x6B65;<code>deliveryTask</code>&#x7684;&#x51FD;&#x6570;&#x5B9E;&#x73B0;&#x3002;</p>
<p>BundleUtil.java</p>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">deliveryTask</span><span class="hljs-params">(<span class="hljs-keyword">boolean</span> sync)</span></span>{
    Runnable installTask = <span class="hljs-keyword">new</span> Runnable() {
        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{
            <span class="hljs-keyword">synchronized</span> (BundleInstaller.<span class="hljs-keyword">this</span>) {
                 <span class="hljs-keyword">try</span>{
                     call();
                 } <span class="hljs-keyword">catch</span> (Throwable e) {
                     e.printStackTrace();
                 } <span class="hljs-keyword">finally</span> {
                     <span class="hljs-keyword">if</span> (mListener != <span class="hljs-keyword">null</span>) {
                         <span class="hljs-keyword">new</span> Handler(Looper.getMainLooper()).post(<span class="hljs-keyword">new</span> Runnable() {
                             <span class="hljs-meta">@Override</span>
                             <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{
                                 mListener.onFinished();
                             }
                 <span class="hljs-comment">//..</span>
        };
        sBundleHandler.post(installTask);

    }
}
</code></pre>
<p>&#x51FD;&#x6570;&#x9996;&#x5148;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x5F02;&#x6B65;&#x4EFB;&#x52A1; <code>installTask</code>,&#x4E4B;&#x540E;&#x5C06;&#x4EFB;&#x52A1;&#x63D0;&#x4EA4;&#x7ED9;<code>sBundleHandler</code>&#x3002;&#x800C;<code>sBundleHandler</code>&#x5B9E;&#x9645;&#x4E0A;&#x5173;&#x8054;&#x7684;&#x662F;&#x4E00;&#x4E2A;HandlerThread,&#x6240;&#x4EE5;&#xFF0C;<code>installTask</code>&#x662F;&#x8FD0;&#x884C;&#x5728;&#x5355;&#x72EC;&#x7684;&#x7EBF;&#x7A0B;&#x4E2D;&#x7684;&#x3002;</p>
<p>&#x5728; <code>installTask</code> &#x4E2D;</p>
<ul>
<li>&#x8C03;&#x7528;&#x4E86;<code>call</code>&#x51FD;&#x6570;</li>
<li>&#x5411;ui&#x7EBF;&#x7A0B;&#x63D0;&#x4EA4;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#xFF0C;&#x7528;&#x4E8E;&#x56DE;&#x8C03;<code>onFinished</code></li>
</ul>
<p>&#x5230;&#x76EE;&#x524D;&#x4E3A;&#x6B62;&#xFF0C;&#x7B2C;&#x4E00;&#x90E8;&#x5206;&#x5206;&#x6790;&#x5B8C;&#x6BD5;&#xFF0C;&#x6211;&#x4EEC;&#x8FDB;&#x5165;&#x7B2C;&#x4E8C;&#x90E8;&#x5206; <strong>&#x52A0;&#x8F7D;&#x8FC7;&#x7A0B;</strong> </p>
<h1 id="&#x7B2C;&#x4E8C;&#x90E8;&#x5206;-&#x52A0;&#x8F7D;&#x8FC7;&#x7A0B;">&#x7B2C;&#x4E8C;&#x90E8;&#x5206; &#x52A0;&#x8F7D;&#x8FC7;&#x7A0B;</h1>
<p>&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;<strong>&#x7B2C;&#x4E8C;&#x90E8;&#x5206;&#x6574;&#x4F53;&#x662F;&#x8FD0;&#x884C;&#x5728; HandlerThread &#x4E2D;&#x7684;&#x3002;</strong></p>
<p>&#x56DE;&#x987E;&#x4E00;&#x4E0B;&#x7B2C;&#x4E8C;&#x90E8;&#x5206;&#x7684;&#x8FD0;&#x884C;&#x65F6;&#x5E8F;</p>
<p><img src="bundle_load_part_2_img.svg" alt=""></p>
<h2 id="call">call</h2>
<pre><code>call(){
    //...
    if (FileUtils.getUsableSpace(Environment.getDataDirectory()) &gt;= 5) {
        //has enough space
        if(AtlasBundleInfoManager.instance().isInternalBundle(bundleName)) {
            bundle = installBundleFromApk(bundleName);
            if (bundle != null) {
                ((BundleImpl) bundle).optDexFile();
            }
        }
    } else {
        throw new LowDiskException(&quot;no enough space&quot;);
    }
    //...
}
</code></pre><p>&#x8FD9;&#x91CC;&#x6709;&#x4E24;&#x4E2A;&#x5224;&#x65AD;&#x6761;&#x4EF6;</p>
<ul>
<li>&#x5269;&#x4F59;&#x5B58;&#x50A8;&#x7A7A;&#x95F4;&#x6EE1;&#x8DB3;</li>
<li>&#x662F;&#x5185;&#x7F6E;bundle</li>
</ul>
<p>&#x5F53;&#x4E24;&#x4E2A;&#x6761;&#x4EF6;&#x90FD;&#x6EE1;&#x8DB3;&#x65F6;&#xFF0C;&#x6267;&#x884C;bundle&#x7684;&#x5B89;&#x88C5;&#x548C;optDexFile&#x64CD;&#x4F5C;&#x3002;</p>
<p>installBundleFromApk &#x53C8;&#x8C03;&#x7528;&#x4E86; installNewBundle&#x65B9;&#x6CD5;&#xFF0C;&#x76F4;&#x63A5;&#x770B;&#x7B2C;9&#x6B65; - installNewBundle &#x7684;&#x5B9E;&#x73B0;&#x3002;</p>
<h2 id="installnewbundle">installNewBundle</h2>
<p>Framework.java</p>
<pre><code>static BundleImpl installNewBundle(final String location, final InputStream in)throws BundleException {
    //...
    BundleListing.BundleInfo info = AtlasBundleInfoManager.instance().getBundleInfo(location);
    BundleImpl bundle = new BundleImpl(bundleDir, location, new BundleContext(), null, file,version,true,-1);
    return bundle;
}
</code></pre><p>&#x51FD;&#x6570;&#x5F88;&#x7B80;&#x5355;&#xFF0C;&#x83B7;&#x53D6;bundle&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x4E4B;&#x540E;&#x6784;&#x9020;&#x4E00;&#x4E2A;BundleImpl&#x5BF9;&#x8C61;&#x3002;</p>
<p>&#x6709;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x4E00;&#x4E0B;</p>
<table>
<thead>
<tr>
<th>&#x53C2;&#x6570;</th>
<th>&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td>bundleDir</td>
<td>/data/data/com.taobao.demo/files/storage/com.taobao.secondbundle</td>
</tr>
<tr>
<td>in</td>
<td>&#x6307;&#x5411; lib/armeabi/libcom_taobao_secondbundle.so</td>
</tr>
</tbody>
</table>
<h2 id="bundleimpl">BundleImpl</h2>
<p>&#x6765;&#x770B;BundleImpl&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;</p>
<p>BundleImpl.java</p>
<pre><code>BundleImpl(final File bundleDir, final String location, final BundleContext context, final InputStream stream,...) throws BundleException, IOException{
        if (stream != null) {
            this.archive = new BundleArchive(location,bundleDir, stream,version, dexPatchVersion);
        } 

        if (autoload) {
            resolveBundle();
            Framework.bundles.put(location, this);
        }
}
</code></pre><p>&#x9996;&#x5148;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;BundleArchive&#x5BF9;&#x8C61;&#xFF0C;bundleArchive&#x6301;&#x6709;bundleDir&#x548C;InputStream&#x7684;&#x5F15;&#x7528;&#xFF0C;&#x7528;&#x6765;&#x505A;dexOpt&#x7684;( <strong>? todo</strong> )&#xFF0C;&#x6682;&#x65F6;&#x4E0D;&#x5173;&#x6CE8;&#xFF0C;&#x5F80;&#x4E0B;&#x8D70;</p>
<h2 id="resolvebundle">resolveBundle</h2>
<p>BundleImpl.java</p>
<pre><code class="lang-java">
    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">synchronized</span> <span class="hljs-keyword">void</span> <span class="hljs-title">resolveBundle</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> BundleException </span>{
        <span class="hljs-comment">//...</span>
        <span class="hljs-keyword">if</span> ( <span class="hljs-keyword">this</span>.classloader == <span class="hljs-keyword">null</span>){
            <span class="hljs-comment">// create the bundle classloader</span>
            List&lt;String&gt; dependencies = AtlasBundleInfoManager.instance().getDependencyForBundle(location);
            String nativeLibDir = getArchive().getCurrentRevision().getRevisionDir().getAbsolutePath()+<span class="hljs-string">&quot;/lib&quot;</span>+<span class="hljs-string">&quot;:&quot;</span>
                    + RuntimeVariables.androidApplication.getApplicationInfo().nativeLibraryDir+<span class="hljs-string">&quot;:&quot;</span>
                    +System.getProperty(<span class="hljs-string">&quot;java.library.path&quot;</span>);
            <span class="hljs-keyword">if</span>(dependencies!=<span class="hljs-keyword">null</span>) {
                <span class="hljs-keyword">for</span> (String str : dependencies) {
                    BundleImpl impl = (BundleImpl) Atlas.getInstance().getBundle(str);
                    <span class="hljs-keyword">if</span> (impl != <span class="hljs-keyword">null</span>) {
                        nativeLibDir += <span class="hljs-string">&quot;:&quot;</span>;
                        File dependencyLibDir = <span class="hljs-keyword">new</span> File(impl.getArchive().getCurrentRevision().getRevisionDir(), <span class="hljs-string">&quot;lib&quot;</span>);
                        nativeLibDir += dependencyLibDir;
                    }
                }
            }
            <span class="hljs-keyword">this</span>.classloader = <span class="hljs-keyword">new</span> BundleClassLoader(<span class="hljs-keyword">this</span>,dependencies,nativeLibDir);
        }

        <span class="hljs-comment">// notify the listeners</span>
        Framework.notifyBundleListeners(<span class="hljs-number">0</span> <span class="hljs-comment">/*LOADED*/</span>, <span class="hljs-keyword">this</span>);
    }
</code></pre>
<p>&#x4E3A;bundle&#x521B;&#x5EFA;&#x4E00;&#x4E2A;classloader&#xFF0C;&#x5728;&#x521B;&#x5EFA;classloader&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;</p>
<ul>
<li>&#x6307;&#x5B9A;bundle&#x81EA;&#x8EAB;&#x4F9D;&#x8D56;so&#x7684;&#x8DEF;&#x5F84;</li>
<li>&#x6307;&#x5B9A;&#x4F9D;&#x8D56;bundle&#x6240;&#x4F9D;&#x8D56;so&#x7684;&#x8DEF;&#x5F84;</li>
</ul>
<p>&#x6BD4;&#x5982;&#x5728;demo&#x4E2D; &#xFF0C;nativeLibDir&#x7684; &#x503C;&#x4E3A; /data/user/0/com.taobao.demo/files/storage/com.taobao.secondbundle/version.1/lib:/data/app/com.taobao.demo-1/lib/x86:/vendor/lib:/system/lib</p>
<p>&#x521B;&#x5EFA;&#x5B8C;BundlerClassLoader&#x540E;&#xFF0C;&#x5728;&#x6700;&#x540E;&#x4E00;&#x884C;&#x8FDB;&#x884C;&#x56DE;&#x8C03;&#xFF0C;&#x8FD9;&#x91CC;&#x7684;&#x56DE;&#x8C03;&#x5B9E;&#x73B0;&#x7C7B;&#x662F; BundleLifecycleHandler</p>
<p>BundleLifecycleHandler.java</p>
<pre><code> public void bundleChanged(final BundleEvent event){
        switch (event.getType()) {
            case 0:/* LOADED */
                loaded(event.getBundle());
}
</code></pre><h2 id="loaded">loaded</h2>
<pre><code>private void loaded(Bundle bundle) { long time = System.currentTimeMillis();
        BundleImpl b = (BundleImpl) bundle;
        DelegateResources.addBundleResources(
            b.getArchive().getArchiveFile().getAbsolutePath()
        );
}
</code></pre><p><strong>todo</strong> &#x8D44;&#x6E90;&#x5904;&#x7406;&#x5B9E;&#x73B0;&#x8BF4;&#x660E;</p>
<p>&#x56DE;&#x5230;&#x7B2C;10&#x6B65;</p>
<p>BundleImpl.java</p>
<pre><code>BundleImpl(final File bundleDir, final String location...) {
    //...
    resolveBundle();
    Framework.bundles.put(location, this);    
}
</code></pre><p>&#x5728;resolveBundle&#x51FD;&#x6570;&#x6267;&#x884C;&#x5468;&#x671F;&#x5185;&#xFF0C;&#x4E3B;&#x8981;&#x5B8C;&#x6210;&#x4E86;&#x4E24;&#x4EF6;&#x4E8B;</p>
<ul>
<li>&#x4E3A;bundle&#x521B;&#x5EFA;&#x4E86;&#x72EC;&#x7ACB;&#x7684;ClassLoader</li>
<li>&#x5C06;bundle&#x4E2D;&#x7684;&#x8D44;&#x6E90;&#x6DFB;&#x52A0;&#x5230;Resource&#x4E0A;</li>
</ul>
<p>&#x7B2C;&#x4E8C;&#x884C;&#x51FD;&#x6570;&#x5C06;bundle&#x7684;&#x4FE1;&#x606F;&#x52A0;&#x5165;&#x5230;&#x5DF2;&#x52A0;&#x8F7D;&#x7684;&#x5217;&#x8868;&#x4E2D;&#x3002;</p>
<p>&#x5B8C;&#x6210;&#x4E4B;&#x540E;&#xFF0C;&#x56DE;&#x5230;&#x7B2C;8&#x6B65;&#xFF0C;&#x6267;&#x884C;optDexFile&#x65B9;&#x6CD5;&#x3002;</p>
<h3 id="optdexfile">optDexFile</h3>
<p>&#x3002;&#x3002;&#x3002;</p>
<h1 id="&#x7B2C;&#x4E09;&#x90E8;&#x5206;-&#x521D;&#x59CB;&#x5316;&#x8FC7;&#x7A0B;">&#x7B2C;&#x4E09;&#x90E8;&#x5206; &#x521D;&#x59CB;&#x5316;&#x8FC7;&#x7A0B;</h1>
<p><strong>&#x6574;&#x4E2A;&#x7B2C;&#x4E09;&#x90E8;&#x5206;&#x8FD0;&#x884C;&#x5728; UI &#x7EBF;&#x7A0B;&#x4E2D;</strong></p>
<p>bundle&#x52A0;&#x8F7D;&#x5B8C;&#x6210;&#x540E;&#xFF0C;&#x7B2C;7&#x6B65;&#xFF0C;&#x5728;UI&#x7EBF;&#x7A0B;&#x4E2D;&#x56DE;&#x8C03;&#x4E86;&#x5B8C;&#x6210;&#x7684;&#x63A5;&#x53E3;&#xFF0C;&#x8C03;&#x7528;&#x4E86;<code>startbundle</code>&#x65B9;&#x6CD5;&#x53C8;&#x7ECF;&#x8FC7;&#x8F97;&#x8F6C;&#x8C03;&#x7528;&#xFF0C;&#x5230;&#x4E86;BundleLifecycleHandler&#x7684;<code>startd</code>&#x65B9;&#x6CD5;&#x4E2D;&#x3002;</p>
<p>BundleLifecycleHandler.java</p>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">started</span><span class="hljs-params">(Bundle bundle)</span></span>{
    BundleImpl b = (BundleImpl) bundle;
    BundleListing.BundleInfo info = AtlasBundleInfoManager.instance().getBundleInfo(b.getLocation());
    <span class="hljs-comment">//...</span>
    String appClassName = info.getApplicationName();
    Application app = newApplication(appClassName, b.getClassLoader());
    app.onCreate();
}
</code></pre>
<p>&#x5728;&#x7B2C;6&#x884C;&#x6784;&#x9020;&#x51FA;bundle&#x4E2D;&#x6CE8;&#x518C;&#x7684;application&#x5BF9;&#x8C61;&#xFF0C;&#x4E4B;&#x540E;&#x6267;&#x884C;application&#x7684;onCreate&#x65B9;&#x6CD5;&#x3002;&#x5BF9;&#x4E8E;appliation&#x6765;&#x8BF4;&#xFF0C;&#x4F3C;&#x4E4E;&#x8FD8;&#x5DEE;&#x4E00;&#x4E2A;&#x5173;&#x952E;&#x7684;<code>attachBaseContext</code>&#x7684;&#x5165;&#x53E3;&#x51FD;&#x6570;&#x8C03;&#x7528;&#xFF0C;&#x6211;&#x4EEC;&#x63A5;&#x7740;&#x770B;&#x6784;&#x9020;&#x8FC7;&#x7A0B;&#x3002;</p>
<p>BundleLifecycleHandler.java</p>
<pre><code>protected static Application newApplication(String applicationClassName, ClassLoader cl) throws ApplicationInitException {
    final Class&lt;?&gt; applicationClass = cl.loadClass(applicationClassName);
    Application app = (Application) applicationClass.newInstance();
    AtlasHacks.Application_attach.invoke(app, RuntimeVariables.androidApplication);
    return app;  
}
</code></pre><p>&#x9996;&#x5148;&#xFF0C;&#x6839;&#x636E;&#x7C7B;&#x540D;&#x52A0;&#x8F7D;&#x5BF9;&#x5E94;&#x7684;class&#xFF0C;&#x4E4B;&#x540E;&#x53CD;&#x5C04;&#x6267;&#x884C;applciation&#x7684;attach&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x5728;<a href="../atlas_start/atlas_start_2.html">Atlas&#x4E4B;&#x542F;&#x52A8;&#x8FC7;&#x7A0B;(&#x4E8C;)</a>&#x4E0B;&#x4E2D;&#xFF0C;&#x89E3;&#x91CA;&#x8FC7;&#xFF0C;application&#x7684;attach&#x65B9;&#x6CD5;&#x6700;&#x7EC8;&#x4F1A;&#x8C03;&#x7528;&#x5230;<code>attachBaseContext</code>&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x6700;&#x540E;&#xFF0C;&#x5927;&#x6982;&#x770B;&#x4E00;&#x4E0B;DelegateClassLoader&#x52A0;&#x8F7D;bundle&#x4E2D;class&#x7684;&#x8FC7;&#x7A0B;&#x3002;</p>
<p>DelegateClassLoader.java</p>
<pre><code>protected Class&lt;?&gt; findClass(String className) throws ClassNotFoundException {
    Class&lt;?&gt; clazz = loadFromInstalledBundles(className,false);
    //...
    return clazz;
}

static Class&lt;?&gt; loadFromInstalledBundles(String className,boolean safe)
            throws ClassNotFoundException {
    String bundleName = AtlasBundleInfoManager.instance().getBundleForComponet(className);
    BundleImpl bundle = (BundleImpl) Atlas.getInstance().getBundle(bundleName);
    //...
    ClassLoader classloader = bundle.getClassLoader();
    Class&lt;?&gt;  clazz = classloader.loadClass(className);
    return clazz;
}
</code></pre><p>&#x53EF;&#x4EE5;&#x770B;&#x5230;&#xFF0C;classloader&#x5148;&#x4ECE;bundle&#x4E0A;&#x53BB;&#x627E;&#x7684;&#x3002;&#x800C;<code>loadFromInstalledBundles</code>&#x903B;&#x8F91;&#x5982;&#x4E0B;:</p>
<ol>
<li>&#x4ECE;<code>AtlasBundleInfoManager</code>&#x4E0A;&#x5DF2;&#x52A0;&#x8F7D;&#x7684;&#x5217;&#x8868;&#x4E2D;&#x627E;&#x5230;&#x5BF9;&#x5E94;bunde(&#x5728;&#x7B2C;&#x5728;&#x7B2C;10&#x6B65;&#x4E2D;&#x6DFB;&#x52A0;)</li>
<li>&#x4ECE;&#x5BF9;&#x5E94;bundle&#x4E0A;&#x7684;classloader&#x4E0A;&#x52A0;&#x8F7D;&#x5BF9;&#x5E94;&#x7684;class&#x3002;</li>
</ol>
<hr>
<p>&#x5230;&#x8FD9;&#x91CC;&#x4E3A;&#x6B62;&#xFF0C;&#x6574;&#x4E2A;bundle&#x7684;&#x52A0;&#x8F7D;&#x3001;&#x521D;&#x59CB;&#x5316;&#x8FC7;&#x7A0B;&#x5206;&#x6790;&#x5B8C;&#x6BD5;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../atlas_start/atlas_start_2.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: Atlas之启动过程(二)">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Atlas之Bundle加载过程","time":"2017-05-05T10:00:00.000Z","level":"1.6.4","depth":2,"previous":{"title":"Atlas之启动过程(二)","level":"1.6.3","depth":2,"path":"code_read/atlas_start/atlas_start_2.md","ref":"code_read/atlas_start/atlas_start_2.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"code_read/atlas_bundle_load/atlas_bundle_load.md","mtime":"2017-09-12T02:32:41.000Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-11-27T10:00:49.012Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

